<!DOCTYPE html>
<html>

<head>
    <{include file="common/header.html" }>
</head>

<body>
    <!-- Preloader -->
    <div id="preloader">
        <div id="status"><i class="fa fa-spinner fa-spin"></i></div>
    </div>
    <section>
        <{include file="common/leftpanel.html" }>
            <!-- leftpanel -->
            <div class="mainpanel">
                <{include file="common/headerbar.html" }>
                    <!-- headerbar -->
                    <div class="pageheader">
                        <h2><i class="fa fa-paper-plane"></i>首页<span>活动管理</span> <span>策略管理</span></h2>
                        <div class="breadcrumb-wrapper">
                            <div class="select-brands-title">所属投放：</div>
                            <div class="select-brands-but" data-is-click="false"><span id="store-title-box" data-plan-id="<{$plan_id}>">全部</span><i class="fa fa-caret-right" style="position:absolute;left:160px;top:0;border:none;margin:0 0 0 10px;"></i></div>
                            <div class="brands-lists-block" style="display:none;">
                                <div class="brands-lists" data-plan-id="">全部</div>
                                <{foreach $plans item=store key=i}>
                                    <div class="brands-lists" data-plan-id="<{$store.plan_id}>"><{$store.plan_name}></div>
                                <{/foreach}>
                            </div>
                        </div>
                    </div>
                    <div class="contentpanel">
                        <div class="panel panel-default">
                            <div class="panel-heading">
                                <div class="page-title">策略管理</div>
                                <div class="date-hook-box">
                                    <i class="fa fa-calendar" style="float:left;margin:8px;"></i>
                                    <a href="javascript:void(0);" id="hook-time-but" data-plan-id="<{$plan_id}>" style="width:80%;"></a>
                                </div>
                                <div class="add-but-block"><a class="add-buts but-24ac45" href="javascript:void(0);" data-plan-id="<{$plan_id}>"><i class="fa fa-plus-circle ml10 mr4"></i>新增策略</a></div>
                                <div class="space-box"></div>
                            </div>
                            <div class="panel-body">
                                <div class="table-responsive">
                                    <div class="table-header-bloxk-box">
                                        <a href="javascript:void(0);" class="select-all-buts" data-is-checked="false"><i class="fa fa-square-o fa-lg"></i>&nbsp;全选</a>
                                        <a href="javascript:void(0);" href="javascript:void(0);" class="delete-all-buts"><i class="fa fa-trash-o fa-lg"></i>&nbsp;批量删除</a>
                                        <a href="javascript:void(0);" class="start-buts"><i class="fa fa-play-circle fa-lg"></i>&nbsp;开启</a>
                                        <a href="javascript:void(0);" class="stop-buts"><i class="fa fa-pause-circle fa-lg"></i>&nbsp;暂停</a>
                                        <span class="sreach-block-box">
                                            <label>搜索：</label><input type="text/submit/hidden/button/etc" name="strategy-search-keyword" value="" placeholder="输入要搜索的内容" id="strategy-search-keyword"><a href="javascript:void(0);" class="strategy-search-buts">确定</a>
                                        </span>
                                    </div>
                                    <table class="table table-striped" id="table2">
                                        <thead>
                                            <tr>          
                                                <th>序号</th>
                                                <th>推广组</th>
                                                <th>推广计划</th>
                                                <th>状态</th>
                                                <th>展现量(次)</th>
                                                <th>点击数(个)</th>
                                                <th>点击率(%)</th>
                                                <th>点击单价(元)</th>
                                                <th>消耗(元)</th>
                                                <th>操作</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <{if $result|@count neq 0}>
                                                <{foreach $result item=lists key=i}>
                                                    <tr>          
                                                        <td><a href="javascript:void(0);" class="select-buts" data-is-checked="false" data-unit-id="<{$lists.unit_id}>"><i class="fa fa-square-o"></i>&nbsp;<{$i+1}></a></td>
                                                        <td><a href="/creativeMange/lists/campaign/creative/0/<{$lists.unit_id}>"><{$lists.unit_name}></a></td>
                                                        <td><{$lists.plan_name}></td>
                                                        <td><{if $lists.status eq 1}>正在推广<{else}>停止推广<{/if}></td>
                                                        <td><{$lists.pv|string_format:"%d"}></td>
                                                        <td><{$lists.click|string_format:"%d"}></td>
                                                        <td><{$lists.ctr|string_format:"%.2f"}>%</td>
                                                        <td><{$lists.click_cost|string_format:"%.2f"}></td>
                                                        <td><{$lists.charge|string_format:"%.2f"}></td>
                                                        <td><a href="javascript:void(0);" data-uid="<{$lists.uid}>" data-shop-id="<{$lists.shop_id}>" data-plan-id="<{$lists.plan_id}>" data-unit-id="<{$lists.unit_id}>" class="delete-buts"><i class="fa fa-trash-o"></i></a><a href="/step/edit/campaign/strategy/2_2/<{$lists.unit_id}>" class="edit-buts"><i class="fa fa-edit"></i></a></td>
                                                    </tr>
                                                <{/foreach}>
                                            <{else}>
                                                <tr><td colspan="12" rowspan="" headers="" style="text-align: center;">暂无数据</td></tr>
                                            <{/if}>
                                            
                                        </tbody>
                                    </table>
                                    <div id="pages-box" data-count="<{$count}>" data-current="<{$current}>"></div>
                                </div>
                            </div>
                        </div>
                    </div>
                    <!-- contentpanel -->
                    <{include file="common/footerbar.html" }>
            </div>
            <!-- mainpanel -->
    </section>
    <script src="/resources/js/libarays/bootstrap.min.js"></script>
    <script src="/resources/js/libarays/jquery.sparkline.min.js"></script>
    <script src="/resources/js/libarays/toggles.min.js"></script>
    <script src="/resources/js/libarays/jquery.cookies.js"></script>
    <script src="/resources/js/libarays/custom.js"></script>
    <script type="text/javascript" src="/resources/js/libarays/laydate/laydate.js" charset="utf-8"></script>
    <script>
    jQuery(document).ready(function() {
        var myDate = new Date();
        var newDate = myDate.getDate();
        var new_time= myDate.getFullYear()+'-'+((myDate.getMonth()+1) > 9 ? (myDate.getMonth()+1) : '0'+(myDate.getMonth()+1))+'-'+(newDate > 9 ? newDate: '0'+newDate);
        var start_date = new_time;
        var end_date = start_date;
        var date_value = new_time+' ~ '+new_time;
        var insl = laydate.render({
            elem: '#hook-time-but',
            max:0,
            range: '~',
            value:date_value,
            done:function(data){
                var start_date = data.split(' ~ ')[0];
                var end_date = data.split(' ~ ')[1];
                var plan_id = $('#hook-time-but').attr('data-plan-id');
                get_strategy_list(plan_id,0,0,0,0,0,start_date,end_date);
                // get_dsp_sadt('today',{
                //     'start_date':start_date,
                //     'end_date':end_date,
                //     'shop_id':shop_id,
                //     'metric':metric,
                //     'format':'chart'
                // });
            }
        });

        get_pages('pages-box','/launchMange/lists/campaign/strategy/1/');

        $(document).on('click','.select-all-buts',function(){
            if($(this).attr('data-is-checked') == 'false'){
                $(this).attr('data-is-checked',true);
                $(this).find('i').removeClass('fa-square-o').addClass('fa-check-square-o');
                $('.select-buts').attr('data-is-checked',true);
                $('.select-buts').find('i').removeClass('fa-square-o').addClass('fa-check-square-o');
            }else{
                $(this).attr('data-is-checked',false);
                $(this).find('i').removeClass('fa-check-square-o').addClass('fa-square-o');
                $('.select-buts').attr('data-is-checked',false);
                $('.select-buts').find('i').removeClass('fa-check-square-o').addClass('fa-square-o');
            }
        });

        $(document).on('click','.select-buts',function(){
            if($(this).attr('data-is-checked') == 'false'){
                $(this).attr('data-is-checked',true);
                $(this).find('i').removeClass('fa-square-o').addClass('fa-check-square-o');
            }else{
                $(this).attr('data-is-checked',false);
                $(this).find('i').removeClass('fa-check-square-o').addClass('fa-square-o');
            }
        });

        $(document).on('click','.select-brands-but',function(e){
            e.stopPropagation();
            if($(this).attr('data-is-click') == 'false'){
                $(this).attr('data-is-click',true);
                $(this).find('i').removeClass('fa-caret-right').addClass('fa-caret-down');
                $('.brands-lists-block').css('display','block');
            }else{
                $(this).attr('data-is-click',false);
                $(this).find('i').removeClass('fa-caret-down').addClass('fa-caret-right');
                $('.brands-lists-block').css('display','none');
            }
            
        });
        $(document).on('click','.brands-lists',function(e){
            $('#store-title-box').html($(this).html()).attr('data-plan-id',$(this).attr('data-plan-id'));
            $('.select-brands-but').attr('data-is-click',false);
            $('.select-brands-but').find('i').removeClass('fa-caret-down').addClass('fa-caret-right');
            $('.brands-lists-block').css('display','none');
            $('.add-buts').attr('data-plan-id',$(this).attr('data-plan-id'));
            get_strategy_list(($(this).attr('data-plan-id') ? $(this).attr('data-plan-id') : 0),0,0,0,0,0,start_date,end_date);
        });

        $(document).on('click','.add-buts',function(){
            if($(this).attr('data-plan-id') == ''){
                ADLINKX.alert('请选择投放');
            }else{
                window.location.href="/step/edit/campaign/strategy/2_1/"+$(this).attr('data-plan-id');
            }
        });


        $(document).on('click','.delete-all-buts',function(){
            var ids = [];
            $('.select-buts').each(function(index, el) {
                if($(this).attr('data-is-checked') == 'true'){
                    ids.push($(this).attr('data-unit-id'));
                }
            });
            if(ids.length > 0){
                delete_strategy(ids.toString());
            }else{
                ADLINKX.alert('请选择要删除的策略');
            }
            
        });

        $(document).on('click','.delete-buts',function(){
            var ids = $(this).attr('data-unit-id');
            delete_strategy(ids);
        });

        $(document).on('click','.start-buts',function(){
            var ids = [];
            $('.select-buts').each(function(index, el) {
                if($(this).attr('data-is-checked') == 'true'){
                    ids.push($(this).attr('data-unit-id'));
                }
            });
            if(ids.length > 0){
                update_strategy(ids.toString(),'start');
            }else{
                ADLINKX.alert('请选择要开启的策略');
            }
        });

        $(document).on('click','.stop-buts',function(){
            var ids = [];
            $('.select-buts').each(function(index, el) {
                if($(this).attr('data-is-checked') == 'true'){
                    ids.push($(this).attr('data-unit-id'));
                }
            });
            if(ids.length > 0){
                update_strategy(ids.toString(),'stop');
            }else{
                ADLINKX.alert('请选择要停止的策略');
            }
        });


        $(document).on('click',function(){
            $('.select-brands-but').attr('data-is-click',false);
            $('.select-brands-but').find('i').removeClass('fa-caret-down').addClass('fa-caret-right');
            $('.brands-lists-block').css('display','none');
        });

        $(document).on('click','.strategy-search-buts',function(){
            var key_words = $('#strategy-search-keyword').val();
            get_strategy_list(0,key_words,0,0,0,0,start_date,end_date);
        });

        function get_strategy_list(plan_id,key_words,offset,num,key,stor,start_date,end_date){
            $.ajax({
                url:encodeURI('/strategyMange/lists/campaign/strategy/1/'+plan_id+'/'+key_words+'/'+offset+'/'+num+'/'+key+'/'+stor+'/'+start_date+'/'+end_date),
                type:'GET',
                success:function(res){
                    console.log(res);
                    if(res.code ==1 && res.msg == 'success'){
                        var html = '';
                        var list = res.data.list;
                        var count = res.data.count;
                        var current = res.data.current;
                        if(list.length > 0){
                            for(var i=0;i<list.length;i++){
                                html += '<tr>'+
                                    '<td><a href="javascript:void(0);" class="select-buts" data-is-checked="false" data-unit-id="'+list[i].unit_id+'"><i class="fa fa-square-o"></i>&nbsp;'+(i+1)+'</a></td>'+
                                    '<td><a href="/creativeMange/lists/campaign/creative/0/'+list[i].unit_id+'" class="">'+list[i].unit_name+'</a></td>'+
                                    '<td>'+list[i].plan_name+'</td>'+
                                    '<td>'+(list[i].status == 1 ? '开启' : '已暂停')+'</td>'+
                                    '<td>'+list[i].pv+'</td>'+
                                    '<td>'+list[i].click+'</td>'+
                                    '<td>'+list[i].ctr+'</td>'+
                                    '<td>'+list[i].click_cost+'</td>'+
                                    '<td>'+list[i].charge+'</td>'+
                                    '<td><a href="javascript:void(0);" data-uid="'+list[i].uid+'" data-shop-id="'+list[i].shop_id+'" data-unit-id="'+list[i].unit_id+'" class="delete-buts"><i class="fa fa-trash-o"></i></a><a href="/step/edit/campaign/strategy/2_2/'+list[i].unit_id+'" class="edit-buts"><i class="fa fa-edit"></i></a></td>'+
                                '</tr>';
                            }
                        }else{
                            html = '<tr><td colspan="12">暂无数据</td></tr>';
                        }
                        $('#table2').find('tbody').html(html);
                        $('#pages-box').attr('data-count',count);
                        $('#pages-box').attr('data-current',current);
                        $('.select-all-buts').find('i').removeClass('fa-check-square-o').addClass('fa-square-o');
                        get_pages('pages-box','/launchMange/lists/campaign/strategy/1/');
                    }else{

                    }
                },
                error:function(err){
                    console.log(err);
                }
            });
        }

        function get_pages(el,url){
            ADLINKX.pages({
                id: el,
                first: {
                    text: '首页',
                    callback: null,
                    enable:true
                },
                last: {
                    text: '尾页',
                    callback: null,
                    enable:true
                },
                next: {
                    text: '下一页',
                    callback: null,
                    enable:true
                },
                previ: {
                    text: '上一页',
                    callback: null,
                    enable:true
                },
                search: {
                    text: '搜索',
                    callback: null,
                    enable:true
                },
                count: $('#'+el).attr('data-count'),
                current: $('#'+el).attr('data-current'),
                num:20,
                color: '#0781ec',
                is_ajax: true,
                url:url,
                callback:function(res){
                    console.log(res);
                }
            });
        }

        function delete_strategy(ids){
            console.log(ids);
            $.ajax({
                url:'/strategyMange/delete',
                type:'POST',
                data:{'ids':ids},
                success:function(res){
                    if(res.code ==1 && res.msg == 'success'){
                        ADLINKX.alert('删除成功',2,function(){
                            window.location.reload();
                        });
                    }else{
                        ADLINKX.alert('删除失败',2,function(){
                            window.location.reload();
                        });
                    }
                },
                error:function(err){
                    console.error(err);
                }
            });
        }

        function update_strategy(ids,action){
            $.ajax({
                url:'/strategyMange/status',
                type:'POST',
                data:{'ids':ids,'action':action},
                success:function(res){
                    if(res.code ==1 && res.msg == 'success'){
                        ADLINKX.alert('操作成功',2,function(){
                            window.location.reload();
                        });
                    }else{
                        ADLINKX.alert('操作失败',2,function(){
                            window.location.reload();
                        });
                    }
                },
                error:function(err){
                     console.error(err);
                },
            });
        }

    });
    </script>
</body>

</html>